// example/select_address/select_address.js
var fun_md5 = require('../../utils/md5.js')
var app = getApp()
import * as Api from '../../utils/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: false,
    addresslist: [],
    addid: '',
    prepareId: ''
  },

  
  selectAddress(e) {

    console.log('修改订单提交的地址')
    let self = this

    let url = app.globalData.baseUrl1 + '/order/updateaddressid';

    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId,
      prepareId: this.data.prepareId,
      addressId: e.currentTarget.dataset.addid
    }
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }

      if (res.data.status != 200) {
        console.log("请求数据错误" + url);
        return;
      }
      if (res.data.status === 200) {

        console.log(res.data)
          wx.navigateBack({
            delta:1
          })
      }
    }
    Api.reqFormPost(url, data, callBack)


  },
  getaddressList() {
    console.log('获取地址')

    let self = this

    let url = app.globalData.baseUrl1 + '/address/list';

    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId
    }
    let callBack = (res) => {

      if (res == null) {

        console.log("网络请求错误！");
        return;
      }

      if (res.data.status != 200) {
        console.log("请求数据错误" + url);
        return;
      }
      if (res.data.status === 200) {
        self.setData({
          addresslist: res.data.data.addresslist
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    this.setData({
      addid: options.addid,
      prepareId: options.prepareId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  changeModel() {
    console.log(111)
    this.setData({
      model: !this.data.model
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    this.getaddressList()
  },
  addEdit(e) {
    switch (e.currentTarget.dataset.type) {
      case 'wxadd':
        wx.chooseAddress({
          success: function (res) {
            wx.navigateTo({
              url: '../add_edit/add_edit?contactname=' + res.userName +
              '&contactmobile=' + res.telNumber +
              '&provinceName=' + res.provinceName +
              '&cityName=' + res.cityName +
              '&countyName=' + res.countyName +
              '&address=' + res.detailInfo +
              '&pagetype=' + 'wxadd'
            })
          }
        })
        break;
      case 'add':
        console.log(111)
        wx.navigateTo({
          url: '../add_edit/add_edit?pagetype=add'
        })
        break;
      case 'edit':
        console.log(e.currentTarget.dataset)

        wx.navigateTo({
          url: '../add_edit/add_edit?contactname=' + e.currentTarget.dataset.contactname +
          '&contactmobile=' + e.currentTarget.dataset.contactmobile +
          '&addressid=' + e.currentTarget.dataset.addressid +
          '&provinceid=' + e.currentTarget.dataset.provinceid +
          '&cityid=' + e.currentTarget.dataset.cityid +
          '&countyid=' + e.currentTarget.dataset.countyid +
          '&address=' + e.currentTarget.dataset.address +
          '&isdefault=' + e.currentTarget.dataset.isdefault +
          '&pagetype=edit'

        })

        break;
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})